import time
from selector.stock import all_stock
from strategy.double_macd import double_macd
from strategy.single_macd import single_macd


strategy_map = {
    "double_macd": double_macd,
    "single_macd": single_macd,
}

class TradeController(object):
    """
    操作
    """

    def __init__(self, id=None, user=None):
        self.id = id
        self.user = user

    def __repr__(self):
        return "graph:{}".format(self.id)

    @classmethod
    def analysis_strategy(cls, strategy_name):
        """使用策略分析所有的gp，返回触发的gp，并发送短信
        parameters:
            strategy: str 策略类型
        """
        # 选取可用的gp
        all_st = all_stock()
        all_st_name = dict(zip(all_st["ts_code"].to_list(), all_st["name"].to_list()))
        strategy_func = strategy_map[strategy_name]
        res = {}
        for num, ts_code in enumerate(all_st["ts_code"]):
            # 验证是否可触发
            res_code = strategy_func(ts_code)
            print(res_code)
            res[ts_code] = {"name": all_st_name[ts_code], "code": res_code}
            if num == 50:
                break
            # if is_able:
            #     # 发送短信
            #     pass
        return res

